﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;

using Kooboo.Web.Url;
using Kooboo.CMS.Sites.Models;
using Kooboo.CMS.Sites.Web;
using Kooboo.CMS.Sites.View;

namespace Kooboo.CMS.Toolkit
{
    public static class FrontUrlHelperExtensions
    {
        /// <summary>
        /// The script file url of current site
        /// </summary>
        /// <param name="frontUrlHelper">The front url helper</param>
        /// <param name="relativeScriptFilePath">The relative script file path of current site</param>
        /// <returns></returns>
        public static IHtmlString ScriptFileUrl(this FrontUrlHelper frontUrlHelper, string relativeScriptFilePath)
        {
            Site site = frontUrlHelper.PageRequestContext.Site;
            return ScriptFileUrl(frontUrlHelper, site, relativeScriptFilePath);
        }

        /// <summary>
        /// The script file url o fsite
        /// </summary>
        /// <param name="frontUrlHelper">The front url helper</param>
        /// <param name="site">The site</param>
        /// <param name="relativeScriptFilePath">The relative script file path of site</param>
        /// <returns></returns>
        public static IHtmlString ScriptFileUrl(this FrontUrlHelper frontUrlHelper, Site site, string relativeScriptFilePath)
        {
            ScriptFile scriptFile = new ScriptFile(site, String.Empty);
            string scriptFileUrl = UrlUtility.Combine(scriptFile.VirtualPath, relativeScriptFilePath);
            scriptFileUrl = frontUrlHelper.Url.Content(scriptFileUrl);
            return frontUrlHelper.ResourceCDNUrl(scriptFileUrl);
        }

        public static IHtmlString SiteScriptsUrl(this FrontUrlHelper frontUrlHelper, Site site)
        {
            return SiteScriptsUrl(frontUrlHelper, site, site.ResourceDomain);
        }

        public static IHtmlString SiteScriptsUrl(this FrontUrlHelper frontUrlHelper, Site site, string baseUri)
        {
            string url = Page_Context.Current.Url.Action("Scripts", "Resource", new { version = site.Version, area = String.Empty });
            string wrapperUrl = FrontUrlHelper.WrapperUrl(url, site, FrontRequestChannel.Debug).ToString();
            return new HtmlString(UrlUtility.ToHttpAbsolute(baseUri, wrapperUrl));
        }
    }
}